# If R packages are not installed, try:
#install.packages("BiocInstaller",
# repos="http://bioconductor.org/packages/3.1/bioc")
#source("http://bioconductor.org/biocLite.R")
#biocLite("DESeq2")
#install.packages("RColorBrewer",contriburl = contrib.url("http://cran.us.r-project.org"))
#install.packages("gplots",contriburl = contrib.url("http://cran.us.r-project.org"))
# load necessary R packages
library(DESeq2)
library(RColorBrewer)
library(gplots)
# source these scripts:
source('plotPCAWithSampleNames.R')
source('overLapper_original.R')
# load the data:
data.1<-read.csv("killifish_allcounts.csv")
# show sample names:
#colnames(data.1)
id<-data.1$GeneID
rownames(data.1)<-id
# show first 6 rows of counts data:
#head(data.1)
# load annotation file
annotation<-read.table("kfish2rae5g.annotation.transcript.name.id", fill=TRUE,header=FALSE)
colnames(annotation)<-c("id","gene")
#head(annotation)
# need to fix this so all gene name words are in one column
# separate only F_diaphanus data
L_parva<-data.1[,c(121:129)]
#colnames(L_parva)
col.names<-colnames(L_parva)
#head(L_parva)
conditions = sapply(strsplit(col.names,"_"),`[`,4)
genus = sapply(strsplit(col.names,"_"),`[`,1)
species = sapply(strsplit(col.names,"_"),`[`,2)
genus_species = paste(genus,species,sep="_")
pop = sapply(strsplit(col.names,"_"),`[`,3)
genus_species_pop = paste(genus_species,pop,sep=".")
genus_species = gsub(".NA", "", genus_species_pop)
ExpDesign <- data.frame(row.names=colnames(L_parva), condition = conditions,genus_species = genus_species)
ExpDesign
cds<-DESeqDataSetFromMatrix(countData=L_parva,
colData=ExpDesign,design= ~ condition)
cds<-DESeq(cds, betaPrior=FALSE)
log_cds<-rlog(cds)
plotPCAWithSampleNames(log_cds,intgroup="condition",ntop=40000)
res.1<-results(cds,contrast=c("condition","BW","FW"))
#dim(res.1)
res.2<-results(cds,contrast=c("condition","transfer","FW"))
res.3<-results(cds,contrast=c("condition","transfer","BW"))
res1_ordered <-as.data.frame(res.1[order(res.1$padj),])
#dim(res1_ordered)
res1_filtered <-subset(res1_ordered,res1_ordered$padj<0.05)
res1_filtered <-subset(res1_filtered,res1_filtered$log2FoldChange>1 | res1_filtered$log2FoldChange< -1)
id<-rownames(res1_filtered)
res1_filtered<-cbind(res1_filtered,id)
dim(res1_filtered)
res2_ordered <-as.data.frame(res.2[order(res.2$padj),])
res2_filtered<-subset(res2_ordered,res2_ordered$padj<0.05)
res2_filtered <-subset(res2_filtered,res2_filtered$log2FoldChange>1 | res2_filtered$log2FoldChange< -1)
id<-rownames(res2_filtered)
res2_filtered<-cbind(res2_filtered,id)
dim(res2_filtered)
res3_ordered<-as.data.frame(res.3[order(res.3$padj),])
res3_filtered<-subset(res3_ordered,res3_ordered$padj<0.05)
res3_filtered <-subset(res3_filtered,res3_filtered$log2FoldChange>1 | res3_filtered$log2FoldChange< -1)
id<-rownames(res3_filtered)
res3_filtered<-cbind(res3_filtered,id)
dim(res3_filtered)
# get normalized counts
# add id column
L_parva_norm_counts<-counts(cds,normalized=TRUE)
id<-rownames(L_parva_norm_counts)
L_parva_norm_counts<-cbind(L_parva_norm_counts,id)
# merge res1, res2, res3 with counts
# "BW","FW"
res1_df<-as.data.frame(res.1)
colnames(res1_df)<-paste(colnames(res1_df),"BW_FW", sep='.')
id<-rownames(res1_df)
res1_df<-cbind(res1_df,id)
# "transfer","FW"
res2_df<-as.data.frame(res.2)
colnames(res2_df)<-paste(colnames(res2_df),"transfer_FW", sep='.')
id<-rownames(res2_df)
res2_df<-cbind(res2_df,id)
# "transfer","BW"
res3_df<-as.data.frame(res.3)
colnames(res3_df)<-paste(colnames(res3_df),"transfer_BW", sep='.')
id<-rownames(res3_df)
res3_df<-cbind(res3_df,id)
L_parva_res<-merge(L_parva_norm_counts,res1_df,by="id")
L_parva_res<-merge(L_parva_res,res2_df,by="id")
L_parva_res<-merge(L_parva_res,res3_df,by="id")
dim(L_parva_res)
L_parva_res<-L_parva_res[complete.cases(L_parva_res),]
dim(L_parva_res)
L_parva_annotated<-merge(L_parva_res,annotation,by="id")
L_parva_annotated<-L_parva_annotated[,c(ncol(L_parva_annotated),1:(ncol(L_parva_annotated)-1))]
#write.csv(L_parva_annotated,"L_parva_results_all.csv")
plot(log2(res.1$baseMean), res.1$log2FoldChange,
col=ifelse(res.1$padj < 0.05, "red","gray67"),
main="L_parva (BW vs. FW) (padj<0.05)",xlim=c(1,15),pch=20,cex=1)
abline(h=c(-1,1), col="blue")
plot(log2(res.2$baseMean), res.2$log2FoldChange,
col=ifelse(res.2$padj < 0.05, "red","gray67"),
main="L_goodei (transfer vs. FW) (padj<0.05)",xlim=c(1,15),pch=20,cex=1)
abline(h=c(-1,1), col="blue")
plot(log2(res.3$baseMean), res.3$log2FoldChange,
col=ifelse(res.3$padj < 0.05, "red","gray67"),
main="L. parva (transfer vs. BW) (padj<0.05)",xlim=c(1,15),pch=20,cex=1)
abline(h=c(-1,1), col="blue")
m<-res1_filtered$id
length(m)
n<-res2_filtered$id
length(n)
o<-res3_filtered$id
length(o)
setlist <- list(BW_FW=as.vector(m),transfer_FW=as.vector(n),transfer_BW=as.vector(o))
OLlist <- overLapper(setlist=setlist, sep="", type="vennsets")
counts <- sapply(OLlist$Venn_List, length)
vennPlot(counts=counts)
# extract intersections:
names(OLlist$Venn_List)
overlap_BW_FWtransfer_FW<-OLlist$Venn_List$BW_FWtransfer_FW
length(overlap_BW_FWtransfer_FW)
overlap_BW_FWtransfer_BW<-OLlist$Venn_List$BW_FWtransfer_BW
length(overlap_BW_FWtransfer_BW)
overlap_transfer_FWtransfer_BW<-OLlist$Venn_List$transfer_FWtransfer_BW
length(overlap_transfer_FWtransfer_BW)
# get lists of unique genes for each comparison
L_parva_BW_FW<-OLlist$Venn_List$BW_FW
length(L_parva_BW_FW)
L_parva_transfer_FW<-OLlist$Venn_List$transfer_FW
length(L_parva_transfer_FW)
L_parva_transfer_BW<-OLlist$Venn_List$transfer_BW
length(L_parva_transfer_BW)